package com.dao.Impl;

import com.bean.Staffs;
import com.dao.StaffsDao;
import com.util.Dbutil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Cc
 * @Date 2024-03-07
 */
public class StaffsDaoImpl implements StaffsDao {
    final Dbutil util = new Dbutil();

    @Override
//    public List<Staffs> selectAll(Integer page, Integer limit) {
//        List<Staffs> staff = new ArrayList<>();
//        String sql = "select * from staffs  limit ?, ?;";
//        Object[] params = { (page - 1) * limit, limit};
//        try {
//            ResultSet rs = util.executeQuery(sql, params);
//
//            while (rs.next()) {
//                PackAgeStaffs(staff, rs);
//            }
//
//            return staff;
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }finally {
//            util.closeAll();
//        }
//    }
    public List<Staffs> selectAll(Integer page, Integer limit) {
        List<Staffs> staffs;
        String sql = "select id, name, age, phone, sta_pos , add_time, update_time from staffs limit ?, ?;";
        Object[] params = { (page - 1) * limit, limit};

        staffs = util.executeQuery(Staffs.class, sql, params);
        return staffs;
    }
    @Override
    public List<Staffs> selectById(int id) {
        List<Staffs> staff = new ArrayList<>();
        String sql = "select * from staffs where id = ?;";
        Object[] params = {id};
        try {
            ResultSet rs = util.executeQuery(sql, params);

            if (rs.next()) {
                PackAgeStaffs(staff, rs);
            }

            return staff;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            util.closeAll();
        }
    }

    /**
     * 封装职员
     *
     * @param staff
     * @param rs
     * @throws SQLException
     */
    private void PackAgeStaffs(List<Staffs> staff, ResultSet rs) throws SQLException {
        Staffs staffs = new Staffs();

        int id = rs.getInt(1);
        String name = rs.getString(2);
        int age = rs.getInt(3);
        String phone = rs.getString(4);
        String staPos = rs.getString(5);
        Timestamp ts = rs.getTimestamp(6);
        Timestamp tsUpdate = rs.getTimestamp(7);

        staffs.setId(id);
        staffs.setName(name);
        staffs.setAge(age);
        staffs.setPhone(phone);
        staffs.setStaPos(staPos);
        staffs.setAddTime(ts);
        staffs.setUpdateTime(tsUpdate);

        staff.add(staffs);
    }

    @Override
    public int insertStaffs(Staffs staffs) {
        String sql = "insert into staffs(name, age, phone, sta_pos, add_time, update_time) values(?,?,?,?,?,?)";
        Object[] params = {
                staffs.getName(),
                staffs.getAge(),
                staffs.getPhone(),
                staffs.getStaPos(),
                staffs.getAddTime(),
                staffs.getUpdateTime()
        };

        try {
            return util.executeUpdate(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            util.closeAll();
        }
    }

    @Override
    public int deleteStaffs(int id) {
        String sql = "delete from staffs where id = ?";
        Object[] params = {id};

        try {
            return util.executeUpdate(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            util.closeAll();
        }
    }

    @Override
    public int updateStaffs(Staffs staffs) {
        String sql = "update staffs set name = ?, age = ?, phone = ?, sta_pos = ?, add_time = ?, update_time = ? where id = ?";
        Object[] params = {
                staffs.getName(),
                staffs.getAge(),
                staffs.getPhone(),
                staffs.getStaPos(),
                staffs.getAddTime(),
                staffs.getUpdateTime(),
                staffs.getId()
        };

        try {
            return util.executeUpdate(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            util.closeAll();
        }
    }
}
